<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel {

    public $username;
    public $password;
    public $password_repeat;
    public $email;
    public $authcode;
    public $mobile;
    public $rememberMe=false;
    private $_identity;
    public $pid;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('username, password', 'required'),
            array('username','length', 'on' => 'register', 'min'=>6, 'max'=>24),
            array('rememberMe', 'boolean'),
            array('password_repeat,email,mobile,pid', 'required', 'on' => 'register'),
            array('email', 'required', 'on' => 'getpwdbyemail'),
            array('mobile', 'required', 'on' => 'getpwdbymobile'),
            array('password_repeat', 'compare', 'on' => 'register','compareAttribute'=>'password', 'message' => '两次密码不一致'),
            array('email', 'email'),
            array('password', 'authenticate', 'on' => 'login'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'username' => '用户名',
            'password' => '密码',
            'password_repeat' => '确认密码',
            'rememberMe' => '记住登录',
            'email' => '邮箱地址',
            'mobile' => '手机号码',
            'authcode' => '验证码',
        );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute, $params) {
        if (!$this->hasErrors()) {
            $this->_identity = new UserIdentity($this->username, $this->password);
            if (!$this->_identity->authenticate())
                $this->addError('password',$this->_identity->errorMessage);
        }
    }

    /**
     * Logs in the user using the given username and password in the model.
     * @return boolean whether login is successful
     */
    public function login() {
        if ($this->_identity === null) {
            $this->_identity = new UserIdentity($this->username, $this->password);
            $this->_identity->authenticate();
        }
        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
            $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days
            Yii::app()->user->login($this->_identity, $duration);
            return true;
        } else
            return false;
    }
    
    public function register() {
        if ($this->_identity === null) {
            $this->_identity = new UserIdentity($this->username, $this->password);
            $this->_identity->register();
        }
        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
            $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days
            Yii::app()->user->login($this->_identity, $duration);
            return true;
        }
        else
            $this->addError('username', $this->_identity->errorMessage);
        return false;
    }
    
    public function updatePassword(){
        if ($this->_identity === null) {
//            $this->_identity = new UserIdentity($_POST['HomeForm']['old_password'], $_POST['HomeForm']['password'], $_POST['HomeForm']['password_repeat']);
//            $this->_identity->updatePassword();
            $api = new Api('account');
            $userid = Yii::app()->user->id;
            $params = array(
                'UpdatePwdByPwdReq' => array(
                    'userId' => $userid,
                    'oldPassWord' => $_POST['HomeForm']['oldpassword'],
                    'passWord' => $_POST['HomeForm']['newpassword'],
                    'confirmPassWord' => $_POST['HomeForm']['newpassword_repeat'],
                )
            );   
            if ($_POST['HomeForm']['newpassword'] == $_POST['HomeForm']['newpassword_repeat']) {
                $row = $api->apiclient('/LdAccountServer/userAccount/updatePwdByPwd', $params);
                if ($row == 0) {
                    throw new CHttpException(404, '网络异常,请稍后再试');
                }
                if ($row['code'] == 0) {
                    return true;
                } else {
                    $this->errorCode = 1;
                    $this->errorMessage = $row['ExceptionRsp']['msg'];
                    return $this->errorCode == 0;
                }
            } else {
                $this->errorCode = 1;
                $this->errorMessage = '两次密码不一致';
                return $this->errorCode == 0;
            }
        }
        return false;
    }

}
